其他
如何驾驭Matplotlib?<Part4>
接下来分享几篇优质Python Matplotlib教程
,可以满足许多领域的可视化需求。
往期回顾:
本期速览:
目录
11、3D图
11.1 3D散点图
11.2 3D散点图设置长宽比
11.3 3D折线图
11.4 3D图设置视角
11.5 3D三角形曲面图
11.6 3D等高线图
11.7 3D等高线图个性化colormap
11.8 更科学的3D等高线图方法
11.9 3D线框图
11.10 3D曲面图
11.11 3D曲面图指定cstride和rstride值
11.12 3D球
11.13 修改3D球视角
12、2D等高线图
12.1 默认2D等高线图
12.2 个性化2D等高线图
11、3D图
该部分通过3D图介绍Matplotlib使用。
11.1 3D散点图
N = 250
np.random.seed(124)
x = 15 * np.random.random(N)
y = np.sin(x) + 0.25 * np.random.random(N)
z = np.cos(x) + 0.25 * np.random.random(N)
plt.figure(figsize=(6, 6), dpi=110)
ax = plt.axes(projection='3d') #开启3D模式
ax.scatter3D(x, y, z, color='r') #3D散点图
ax.set_xlabel('x', fontsize=20, labelpad=20)
ax.set_ylabel('y', fontsize=20, labelpad=20)
ax.set_zlabel('z', fontsize=20, labelpad=20)
11.2 3D散点图设置长宽比
N = 250
np.random.seed(124)
x = 15 * np.random.random(N)
y = np.sin(x) + 0.25 * np.random.random(N)
z = np.cos(x) + 0.25 * np.random.random(N)
plt.figure(figsize=(6, 6), dpi=110)
ax = plt.axes(projection='3d') #开启3D模式
ax.scatter3D(x, y, z, color='r') #3D散点图
ax.set_xlabel('x', fontsize=20, labelpad=20)
ax.set_ylabel('y', fontsize=20, labelpad=20)
ax.set_zlabel('z', fontsize=20, labelpad=20)
ax.set_box_aspect((2., 1.5, 1.2)) #设置长宽比
11.3 3D折线图
N = 100
np.random.seed(124)
xline = np.linspace(0, 15, N)
yline = np.sin(xline)
zline = np.cos(xline)
fig = plt.figure(figsize=(9, 6), dpi=110)
ax = plt.axes(projection='3d')
ax.plot3D(xline, yline, zline) #plot3D绘制折线图
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_box_aspect((2, 1.5, 1.2))
11.4 3D图设置视角
N = 250
np.random.seed(124)
x = 15 * np.random.random(N)
y = np.sin(x) + 0.25 * np.random.random(N)
z = np.cos(x) + 0.25 * np.random.random(N)
plt.figure(figsize=(6, 6), dpi=110)
ax = plt.axes(projection='3d')
ax.scatter3D(x, y, z, color='r')
ax.set_xlabel('x', fontsize=20, labelpad=20)
ax.set_ylabel('y', fontsize=20, labelpad=20)
ax.set_zlabel('z', fontsize=20, labelpad=20)
ax.set_box_aspect((2., 1.5, 1.2))
ax.view_init(10, 180) #设置3D图角度,10是仰角,180是方位角
11.5 3D三角形曲面图
N = 2000
np.random.seed(124)
r = 2 * np.pi * np.random.random(N)
theta = 20 * np.pi * np.random.random(N)
xdata = np.ravel(r * np.sin(theta))
ydata = np.ravel(r * np.cos(theta))
zdata = np.sin(xdata) + np.cos(ydata)
fig = plt.figure(figsize=(15, 6), dpi=110)
plt.subplots_adjust(wspace=0)
ax1 = plt.subplot(121, projection='3d')
ax1.plot_trisurf(xdata, ydata, zdata, cmap='inferno') #.plot_trisurf绘制3D曲面
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')
ax1.view_init(40, 100)
ax1.set_box_aspect((4.5, 4.5, 1.5))
ax1.set_title('Elevation = 40$^\circ$, Azimuth = 100$^\circ)
ax2 = plt.subplot(122, projection='3d')
ax2.plot_trisurf(xdata, ydata, zdata, cmap='inferno')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_zlabel('z')
ax2.view_init(20, 100)
ax2.set_box_aspect((4.5, 4.5, 1.5))
ax2.set_title('Elevation = 20$^\circ$, Azimuth = 100$^\circ)
11.6 3D等高线图
N = 100
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
fig = plt.figure(figsize=(9, 6), dpi=110)
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, cmap='Spectral') #contour3D绘制等高线图
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_box_aspect((2, 2, 1))
ax.view_init(60, 100)
ax.set_title('Contour counts = Default, elevation = 60, azimuth = 100')
11.7 3D等高线图个性化colormap
N = 100
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
fig = plt.figure(figsize=(20, 6), dpi=110)
ax = plt.axes(projection='3d')
p = ax.contour3D(
X,
Y,
Z,
256,
cmap='Spectral' #个性化colormap
)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_box_aspect((3, 3, 1))
ax.view_init(60, 100)
plt.colorbar(p)
ax.set_title('Contour counts = 256, elevation = 60, azimuth = 100')
11.8 更科学的3D等高线图方法
N = 100
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
plt.figure(figsize=(14, 6), dpi=110)
ax1 = plt.subplot(121, projection='3d')
ax1.contour(X, Y, Z, cmap='Spectral') #更好的3D等高线方法contour
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')
ax1.set_box_aspect((3, 3, 1))
ax1.view_init(10, 100)
ax1.set_title('Contour Default, elevation = 10, azimuth = 100')
ax2 = plt.subplot(122, projection='3d')
ax2.contourf(X, Y, Z, cmap='Spectral') #更好的3D等高线方法contourf
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_zlabel('z')
ax2.set_box_aspect((3, 3, 1))
ax2.view_init(10, 100)
ax2.set_title('Contourf Default, elevation = 10, azimuth = 100')
11.9 3D线框图
N = 100
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
fig = plt.figure(figsize=(6, 6), dpi=110)
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color='k', alpha=.2) #plot_wireframe
11.10 3D曲面图
N = 100
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
fig = plt.figure(figsize=(8, 8), dpi=110)
ax = plt.axes(projection='3d') # 3d contour plot
ax.plot_surface(
X,
Y,
Z,
)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_box_aspect((2, 2, 1))
ax.view_init(10, 100)
ax.set_title('Plot surface Default, elevation = 10, azimuth = 100')
11.11 3D曲面图指定cstride和rstride值
N = 200
np.random.seed(3124)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
fig = plt.figure(figsize=(14, 6))
ax1 = plt.subplot(121, projection='3d') # 3d contour plot
ax1.plot_surface(X, Y, Z, cmap='Spectral')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')
ax1.set_box_aspect((2, 2, 1))
ax1.view_init(60, 100)
ax1.set_title(
'Plot surface rstride = cstride = default, \n elevation = 60, azimuth = 100'
)
ax2 = plt.subplot(122, projection='3d') # 3d contour plot
ax2.plot_surface(X, Y, Z, cmap='Spectral', rstride=1,
cstride=1) #指定cstride和rstride的值
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_zlabel('z')
ax2.set_box_aspect((2, 2, 1))
ax2.view_init(60, 100)
ax2.set_title(
'Plot surface rstride = cstride = 1, \n elevation = 60, azimuth = 100')
11.12 3D球
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
plt.figure(figsize=(10, 10))
ax = plt.subplot(projection='3d')
ax.plot_surface(x, y, z, cmap='inferno') #plot_surface
11.13 修改3D球视角
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
rows = 2
columns = 2
grid = plt.GridSpec(rows, columns, wspace=.2, hspace=.2)
elev = np.arange(0, 40, 10)
azim = np.arange(0, 200, 50)
plt.figure(figsize=(12, 12))
for i in range(rows * columns):
ax = plt.subplot(grid[i], projection='3d')
ax.plot_surface(x, y, z, cmap='inferno')
ax.view_init(elev[i], azim[i]) #修改视角
ax.set_title('Elevation = ' + str(elev[i]) + ', Azimuth = ' + str(azim[i]))
12、2D等高线图
该部分通过2D等高线图学习Matplotlib。
12.1 默认2D等高线图
N = 100
np.random.seed(100)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
plt.figure(figsize=(7, 5), dpi=110)
plt.contour(X, Y, Z) #contour
plt.title('Contour 2D Default', pad=10)
12.2 个性化2D等高线图
N = 100
np.random.seed(100)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
plt.figure(figsize=(15, 5))
plt.subplot(121)
plt.contour(X, Y, Z, 256)
plt.title('Contour 2D counts = 256, cmap = viridis', pad=10)
plt.colorbar()
plt.subplot(122)
plt.contour(X, Y, Z, 256, cmap='Spectral')
plt.colorbar()
plt.title('Contour 2D counts = 256, cmap = Spectral', pad=10)
N = 100
np.random.seed(100)
x = np.linspace(-2, 2, N) + np.random.random(N)
y = np.linspace(-2, 2, N) + np.random.random(N)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)**3
plt.figure(figsize=(15, 5))
plt.subplot(121)
plt.contourf(X, Y, Z, 50, cmap = 'inferno')
plt.colorbar()
plt.title('Contourf 2D counts = 50', pad = 10)
plt.subplot(122)
plt.contourf(X, Y, Z, 200, cmap = 'inferno')
plt.colorbar()
plt.title('Contourf 2D counts = 200', pad = 10)
python可视化52|最有价值50图表(python实现代码分享) 详解Python列表推导式|迭代器|生成器|匿名函数 Jupyter Notebook的16个超棒插件! 详解Python Matplotlib (pyplot法) 一图胜千言,超形象图解NumPy教程!
微信交流
备注来意(交流、合作等等)